//******************************************************************************
// @File: cyzSteppingAction.cc
// @Author: Cyz
// @Date: 2020年 5月 4日 星期一 22时12分58秒 CST
// @Description: 
//		Implementation of the cyzSteppingAction class
//******************************************************************************

#include "cyzSteppingAction.hh"
#include "cyzEventAction.hh"
#include "cyzDetectorConstruction.hh"

#include "G4Step.hh"
#include "G4Event.hh"
#include "G4RunManager.hh"
#include "G4LogicalVolume.hh"

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

cyzSteppingAction::cyzSteppingAction(cyzEventAction* eventAction,
																		 cyzDetectorConstruction* detector)
: G4UserSteppingAction(),
	fDetectorConstruction(detector),
  fEventAction(eventAction),
	fEdep(0)
{
	fPoint = G4ThreeVector(0,0,0);
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

cyzSteppingAction::~cyzSteppingAction()
{}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

void cyzSteppingAction::UserSteppingAction(const G4Step* step)
{
	//
	// Get Scoring Volume
	//
	G4LogicalVolume* fHead = fDetectorConstruction->GetHead();
	G4LogicalVolume* fBody = fDetectorConstruction->GetBody();

  // 
	// get volume of the current step
	//
  G4LogicalVolume* volume 
    = step->GetPreStepPoint()->GetTouchableHandle()
      ->GetVolume()->GetLogicalVolume();
  
	//
	// get needed data
	//
	fEdep = step->GetTotalEnergyDeposit();
	fPoint = step->GetPreStepPoint()->GetPosition();	

	//
	// check if we are in scoring volume
	//
	/********  Check if in Head  ********/
  if (volume == fHead) {
		fEventAction->AddfEdep_H(fEdep);
		fEventAction->AddfEcenter_H(fPoint*fEdep);
	}

	/********  Check if in Body  ********/
	else {
		if (volume == fBody){
			fEventAction->AddfEdep_B(fEdep);
			fEventAction->AddfEcenter_B(fEdep*fPoint);
		}
	}

	//
	// Check if it's first step
	//
	if (fEventAction->GetifInitDirection()){
		fEventAction->SetDirection(step->GetPostStepPoint()
				->GetMomentumDirection());
	}
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

